import {
	IFormDetailAbility,
	IFormDetailController,
	IFormDetailControllerParams,
	IParam,
} from '@/core/interface';

/**
 * 表单成员控制器基类
 *
 * @export
 * @class FormDetailController
 * @implements {IFormDetailController<A>}
 * @template A
 */
export class FormDetailController<A extends IFormDetailAbility>
	implements IFormDetailController<A>
{
	/**
	 * 控制器名称
	 *
	 * @protected
	 * @type {string}
	 * @memberof FormDetailController
	 */
	protected name!: string;

	/**
	 * 成员类型
	 *
	 * @protected
	 * @type {string}
	 * @memberof FormDetailController
	 */
	protected detailType!: string;

	/**
	 * 成员标题
	 *
	 * @protected
	 * @type {string}
	 * @memberof FormDetailController
	 */
	protected caption!: string;

	/**
	 * 成员模型数据
	 *
	 * @protected
	 * @type {IParam}
	 * @memberof FormDetailController
	 */
	protected readonly model: IParam = {};

	/**
	 * 成员能力块
	 *
	 * @protected
	 * @type {IFormDetailAbility}
	 * @memberof FormDetailController
	 */
	protected ability!: IFormDetailAbility;

	/**
	 * 成员是否显示
	 *
	 * @protected
	 * @type {boolean}
	 * @memberof FormDetailController
	 */
	visible = true;

	/**
	 * 成员是否必填
	 *
	 * @type {boolean}
	 * @memberof FormDetailController
	 */
	required = false;

	/**
	 * 成员是否禁用
	 *
	 * @protected
	 * @type {boolean}
	 * @memberof FormDetailController
	 */
	disabled = false;

	/**
	 * 容器显示的更多项
	 *
	 * @type {string[]}
	 * @memberof FormDetailController
	 */
	showMoreItems: string[] = [];

	/**
	 * 启用模式 (不启用 | 新建 | 更新 | 启用)
	 *
	 * @type {(0 | 1 | 2 | 3)}
	 * @memberof FormDetailController
	 */
	enableCond: 0 | 1 | 2 | 3 = 3;

	/**
	 * 显示更多模式 (无 | 受控内容 | 管理容器 )
	 *
	 * @protected
	 * @type {(0 | 1 | 2)}
	 * @memberof FormDetailController
	 */
	readonly showMoreMode: 0 | 1 | 2 = 0;

	/**
	 * 忽略值输入
	 *
	 * @type {(0 | 1| 2| 3 | 4)}
	 * @memberof FormDetailController
	 */
	ignoreInput: 0 | 1 | 2 | 3 | 4 = 0;

	/**
	 * 表单按钮行为
	 *
	 * @type {IParam}
	 * @memberof FormDetailController
	 */
	uIAction?: IParam;

	/**
	 * 表单分组行为组
	 *
	 * @type {IParam}
	 * @memberof FormDetailController
	 */
	uIActionGroup?: IParam;

	/**
	 * Creates an instance of FormDetailController.
	 * @param {IFormDetailControllerParams} params
	 * @memberof FormDetailController
	 */
	constructor(params: IFormDetailControllerParams) {
		this.model = params.model;
		this.name = params.model.name;
		this.detailType = params.model.detailType;
		this.showMoreMode = params.model.showMoreMode;
		this.showMoreItems = params.model.showMoreItems || [];
		this.uIAction = params.model.uIAction;
		this.uIActionGroup = params.model.uIActionGroup;
		this.enableCond = params.model.enableCond;
		this.detailInit();
	}

	/**
	 * 成员初始化
	 *
	 * @memberof FormDetailController
	 */
	detailInit() {
		this.visible = this.model.visible || true;
		this.required = this.model.required || false;
		this.disabled = this.model.disabled || false;
		if (this.model.groupLogics) {
			const panelVisible = this.model.groupLogics.find(
				(logic: IParam) => logic.logicCat === 'PANELVISIBLE'
			);
			const itemBlank = this.model.groupLogics.find(
				(logic: IParam) => logic.logicCat === 'ITEMBLANK'
			);
			const itemEnable = this.model.groupLogics.find(
				(logic: IParam) => logic.logicCat === 'ITEMENABLE'
			);
			if (panelVisible) {
				this.visible = false;
			}
			if (itemBlank) {
				this.required = false;
			}
			if (itemEnable) {
				this.disabled = true;
			}
		}
	}

	/**
	 * 获取能力
	 *
	 * @template A
	 * @return {*}  {A}
	 * @memberof FormDetailController
	 */
	getAbility(): A {
		return this.ability as A;
	}

	/**
	 * 设置能力
	 *
	 * @param {IFormDetailAbility} ability
	 * @memberof FormDetailController
	 */
	setAbility(ability: IFormDetailAbility): void {
		this.ability = ability;
	}

	/**
	 * 获取模型
	 *
	 * @template T
	 * @return {*}  {T}
	 * @memberof FormDetailController
	 */
	getModel<T>(): T {
		return this.model as T;
	}
}
